function showStatusMessage(msg)
{
	var smc = $("#statusMessageContainer");
	var smt = $("#statusMessage");
	smc.css("display", "block");
	smt.text(msg);
	smc.animate({top: "-5"}, 1000, function() {});
	window.setTimeout(function () {
		smc.animate({top: "-100"}, 1000, function() {smc.css("display", "none");});
	}, 3000);
}

function itemWidth(item) 
{
	var width = item.css("width");
	var lmargin = item.css("margin-left");
	var rmargin = item.css("margin-right");
	
	return parseFloat(width) + parseFloat(lmargin) + parseFloat(rmargin) + 5;
}

function enableScrollers()
{
	var rscroller = $(".right_nav");
	var lscroller = $(".left_nav");
	
	rscroller.fadeOut(0, function() {});
	lscroller.fadeOut(0, function() {});
	
	rscroller.each(function() {
		var parent = $(this).parent();
		var tns = $(".section_data", parent);
		var count = tns.length;
		
		if (count > 3)
			$(this).fadeIn(0, function() {});
	});
}

function updateCounts()
{
	var banner = $(".number_banner");
	banner.each(function () {
		var parent = $(this).parent().parent();
		var tns = $(".section_data", parent);
		var count = tns.length;
		
		$(this).text(count);
	});
}

$(document).ready(function() {
	enableScrollers();
	updateCounts();
	
	/* Search button action */
	$(".search_submit").click(function() {
		showStatusMessage("Search is not enabled - please try later");
	});
	
	/* Add DVD button */
	$("#addDVDButton").click(function() {
		showStatusMessage("DVDs cannot be added at this point - please try later");
	});
	
	$(".user_input").focus(function() {
		var text = $(this).val();
		var fs = $(this).css("font-style");
		var name = $(this).attr('name');
		
		if (text == name && fs == "italic") {
			$(this).val("");
			$(this).css("font-style", "normal");
			$(this).css("color", "black");
		}
	});
	
	$(".user_input").blur(function() {
		var text = $(this).val();
		var name = $(this).attr('name');
		if (text == "") {
			$(this).val(name);
			$(this).css("font-style", "italic");
			$(this).css("color", "gray");
		}
	});
	
	$(".user_nav_menu").click(function() {
		var layer = $(".nav_container");
		var list = $("#user_menu");
		
		if (list.css("display") == "none") {
			list.css("display", "block");
			layer.css("background", "rgb(221, 221, 221)");
			layer.css("-webkit-box-shadow", "0px 1px 2px black");
			layer.css("box-shadow", "0px 1px 2px black");
		}
		else {
			list.css("display", "none");
			layer.css("background", "");
			layer.css("-webkit-box-shadow", "none");
			layer.css("box-shadow", "none");
		}
	});
	
	$(".left_nav").click(function() {
		var parent = $(this).parent();
		var tns = $(".section_data", parent);
		var count = tns.length;
		var left = parseFloat(tns.css("left"));
		var width = itemWidth(tns);
		var scroll = left / width;

		++scroll;
		
		tns.animate({
			left: '+=' + width
		}, 1000, function() {});

		/* Disable left scroller when we have no items on the left */
		if (scroll == 0) {
			$(this).fadeOut(500, function() {});
		}

		/* If more than 3 items are visible, enable the right scroller */
		if (scroll == -(count - 4)) {
			$(".right_nav", parent).fadeIn(500, function() {});
		}
	});
	
	$(".right_nav").click(function() {
		var parent = $(this).parent();
		var tns = $(".section_data", parent);
		var count = tns.length;
		var left = parseFloat(tns.css("left"));
		var width = itemWidth(tns);
		var scroll = left / width;

		--scroll;
		
		tns.animate({
			left: '-=' + width
		}, 1000, function() {});

		if (scroll == -1) {
			$(".left_nav", parent).fadeIn(500, function() {});
		}

		/* Disable the right scroller when we have only 3 items visible */
		if (scroll == -(count - 3)) {
			$(this).fadeOut(500, function() {});
		}
	});
});
